#!/bin/bash

# Cookie 测试脚本
# 用于诊断 nginx 代理和 Cookie 传递问题

echo "=========================================="
echo "Cookie 传递诊断脚本"
echo "=========================================="
echo ""

# 配置
API_BASE="http://101.34.40.20/api"
COOKIE_FILE="/tmp/test_cookies.txt"

# 清理旧的 Cookie 文件
rm -f $COOKIE_FILE

echo "步骤 1: 测试登录接口"
echo "----------------------------------------"
echo "请求: POST $API_BASE/user/login"
echo ""

# 提示用户输入账号密码
read -p "请输入账号: " USER_ACCOUNT
read -sp "请输入密码: " USER_PASSWORD
echo ""

# 发送登录请求
RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" -X POST "$API_BASE/user/login" \
  -H "Content-Type: application/json" \
  -d "{\"userAccount\":\"$USER_ACCOUNT\",\"userPassword\":\"$USER_PASSWORD\"}" \
  -c $COOKIE_FILE \
  -v 2>&1)

HTTP_CODE=$(echo "$RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
RESPONSE_BODY=$(echo "$RESPONSE" | sed '/HTTP_CODE:/d' | sed '/^</d' | sed '/^>/d' | sed '/^* /d' | sed '/^curl/d')

echo "响应状态码: $HTTP_CODE"
echo ""

# 检查响应头中的 Set-Cookie
echo "检查 Set-Cookie 响应头:"
if echo "$RESPONSE" | grep -i "set-cookie" > /dev/null; then
    echo "✅ 找到 Set-Cookie 响应头:"
    echo "$RESPONSE" | grep -i "set-cookie"
else
    echo "❌ 未找到 Set-Cookie 响应头"
fi
echo ""

# 检查 Cookie 文件
echo "检查保存的 Cookie:"
if [ -f $COOKIE_FILE ]; then
    echo "✅ Cookie 文件已创建:"
    cat $COOKIE_FILE
else
    echo "❌ Cookie 文件未创建"
fi
echo ""

if [ "$HTTP_CODE" != "200" ]; then
    echo "❌ 登录失败，状态码: $HTTP_CODE"
    echo "响应内容:"
    echo "$RESPONSE_BODY"
    exit 1
fi

echo "✅ 登录成功"
echo ""

echo "步骤 2: 测试获取用户信息接口（使用 Cookie）"
echo "----------------------------------------"
echo "请求: GET $API_BASE/user/get/login"
echo ""

# 发送获取用户信息请求
USER_INFO_RESPONSE=$(curl -s -w "\nHTTP_CODE:%{http_code}" "$API_BASE/user/get/login" \
  -b $COOKIE_FILE \
  -v 2>&1)

USER_INFO_HTTP_CODE=$(echo "$USER_INFO_RESPONSE" | grep "HTTP_CODE:" | cut -d: -f2)
USER_INFO_BODY=$(echo "$USER_INFO_RESPONSE" | sed '/HTTP_CODE:/d' | sed '/^</d' | sed '/^>/d' | sed '/^* /d' | sed '/^curl/d')

echo "响应状态码: $USER_INFO_HTTP_CODE"
echo ""

# 检查请求头中的 Cookie
echo "检查请求头中的 Cookie:"
if echo "$USER_INFO_RESPONSE" | grep -i "cookie:" > /dev/null; then
    echo "✅ 请求头中包含 Cookie:"
    echo "$USER_INFO_RESPONSE" | grep -i "cookie:"
else
    echo "❌ 请求头中未包含 Cookie"
fi
echo ""

if [ "$USER_INFO_HTTP_CODE" = "200" ]; then
    echo "✅ 获取用户信息成功"
    echo "响应内容:"
    echo "$USER_INFO_BODY" | head -20
elif [ "$USER_INFO_HTTP_CODE" = "401" ]; then
    echo "❌ 获取用户信息失败，返回 401（未授权）"
    echo "这通常意味着："
    echo "  1. Cookie 没有正确传递到后端"
    echo "  2. 后端 Session 已过期或无效"
    echo "  3. nginx 配置问题（proxy_cookie_path 等）"
    echo ""
    echo "响应内容:"
    echo "$USER_INFO_BODY"
else
    echo "❌ 获取用户信息失败，状态码: $USER_INFO_HTTP_CODE"
    echo "响应内容:"
    echo "$USER_INFO_BODY"
fi
echo ""

echo "=========================================="
echo "诊断完成"
echo "=========================================="
echo ""
echo "如果获取用户信息返回 401，请检查："
echo "  1. nginx 配置中的 proxy_cookie_path"
echo "  2. nginx 配置中的 proxy_cookie_flags"
echo "  3. 后端 CORS 配置"
echo "  4. 后端 Session 配置"
echo ""

